package com.lingkang.flychat.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lingkang.flychat.FlyChatApplication;
import com.lingkang.flychat.R;
import com.lingkang.flychat.entity.ContactsFriend;
import com.lingkang.flychat.entity.MessageList;
import com.lingkang.flychat.mapper.gen.DaoSession;
import com.lingkang.flychat.mapper.gen.MessageListDao;
import com.lingkang.flychat.ui.activity.chat.FriendChatActivity;
import com.lingkang.flychat.ui.activity.main.MainActivityInit;
import com.lingkang.flychat.ui.adapter.ContactsGroupAdapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FragmentContacts extends Fragment {

    //    @BindView(R.id.elv_contacts)
    ExpandableListView elvContacts;
    //Model：定义的数据
    /*public static List<ContactsGroup> groups = new ArrayList<>();
    public static List<ContactsFriendChilds> childs = new ArrayList<>();
    public static List<FriendList> friendLists = new ArrayList<>();*/

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        ButterKnife.bind(getActivity());
        return LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home_contacts, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        initView();
    }

    private void initView() {
        elvContacts = getActivity().findViewById(R.id.elv_contacts);
        ContactsGroupAdapter adapter = new ContactsGroupAdapter(getContext(), MainActivityInit.groups, MainActivityInit.childs);
        elvContacts.setAdapter(adapter);
        // 获取对象
        DaoSession daoSession = ((FlyChatApplication) getActivity().getApplication()).getDaoSession();
        MessageListDao messageListDao = daoSession.getMessageListDao();
        elvContacts.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                ContactsFriend friend = MainActivityInit.childs.get(groupPosition).getContactsFriends().get(childPosition);
                List<MessageList> lists = new ArrayList<>();
                lists = messageListDao.queryRaw("where USERNAME=?", friend.getUsername());
                if (lists.isEmpty()) {
                    MessageList list = new MessageList();
                    list.setUsername(friend.getUsername());
                    list.setImg(friend.getImg());
                    list.setTitle(friend.getName());
                    list.setType(1);
                    list.setLastTime(new Date());
                    messageListDao.insert(list);
                } else {
                    MessageList list = lists.get(0);
                    list.setLastTime(new Date());
                    messageListDao.update(list);
                }
                Intent intent = new Intent(getActivity(), FriendChatActivity.class);
                intent.putExtra("childPosition", childPosition);
                intent.putExtra("groupPosition", groupPosition);
                startActivity(intent);
                return false;
            }
        });
    }
}
